<!--  -->
<template>
  <div class="container">
    <div class="navheader borderbox" style="border-bottom: none">
      <img class="back" src="@/assets/img/icon-back.png" @click="goBack" />
    </div>
    <div class="password-login-form">
      <div class="password-login-title">登录</div>
      <div class="item">
        <div class="name">手机号</div>
        <div class="input">
          <input
            type="number"
            class="input-text"
            placeholder="请输入手机号码"
            v-model="loginForm.phoneNumber"
          />
          <img
            v-if="loginForm.phoneNumber"
            src="@/assets/img/new/close.png"
            style="width: 16px; height: 16px"
            @click="cleanPhoneNumber"
          />
        </div>
      </div>
      <div class="item">
        <div class="name">密码</div>
        <div class="input">
          <input
            type="password"
            class="input-text"
            placeholder="请输入密码"
            v-model="loginForm.password"
          />
          <img
            v-if="loginForm.password"
            src="@/assets/img/new/close.png"
            style="width: 16px; height: 16px"
            @click="cleanPassword"
          />
        </div>
      </div>
    </div>

    <div class="box border-box mt-15 pl-60 pr-60">
      <div
        v-if="loginForm.phoneNumber && loginForm.password"
        class="btn-confirm active"
        @click="login"
      >
        登录
      </div>
      <div v-else class="btn-confirm">登录</div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { Toast } from "vant";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      url: this.$route.query.url || "",
      loginForm: {
        phoneNumber: "",
        password: "",
      },
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    cleanPhoneNumber() {
      this.loginForm.phoneNumber = "";
    },
    cleanPassword() {
      this.loginForm.password = "";
    },
    login() {
      if (!this.loginForm.password) {
        Toast.fail("密码不能为空!");
        return;
      }
      if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.loginForm.phoneNumber)) {
        Toast.fail("手机号码格式不正确!");
        return;
      }
      this.$request
        .post("/api/v2/login/password", {
          mobile: this.loginForm.phoneNumber,
          password: this.loginForm.password,
        })
        .then((res) => {
          if (res.code == 0) {
            let token = res.data.token;
            localStorage.setItem("myToken", token);
          }
          if (this.url) {
            window.location.href = this.url;
          } else {
            this.$router.go(-1);
            // console.log(window.location.host);
            // window.location.href = "http://" + window.location.host;
            // console.log(window.location.href);
          }
        })
        .catch((err) => {
          Toast.fail(err);
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="less" scoped>
.container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  overflow-x: hidden;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding-top: 50px;
  background: #fff;
}
.navheader {
  position: fixed;
  top: 0;
  width: 100%;
  height: 50px;
  background-color: #fff;
  z-index: 100;
  padding-left: 15px;
  padding-right: 15px;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  border-bottom: 1px solid #f1f2f6;
}

.mr-5 {
  margin-right: 5px;
}

.mr-10 {
  margin-right: 10px;
}

.ml-5 {
  margin-left: 5px;
}

.ml-10 {
  margin-left: 10px;
}

.mt-10 {
  margin-top: 10px;
}

.mt-15 {
  margin-top: 15px;
}

.pl-30 {
  padding-left: 15px;
}

.pr-30 {
  padding-right: 15px;
}

.pl-60 {
  padding-left: 30px;
}

.pr-60 {
  padding-right: 30px;
}

.navheader .back {
  position: absolute;
  left: 15px;
  width: 19px;
  height: 19px;
}

.navheader .title {
  width: 72%;
  height: auto;
  font-size: 17px;
  font-weight: 500;
  text-align: center;
  color: #333;
  line-height: 22px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.box {
  width: 100%;
  height: auto;
  float: left;
  box-sizing: border-box;
}
.btn-confirm {
  width: 100%;
  height: 48px;
  background-color: rgba(#3ca7fa, 0.6);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 500;
  color: #ffffff;
  cursor: pointer;
  &.active {
    background: #3ca7fa;
  }
}

.password-login-form {
  width: 100%;
  height: auto;
  float: left;
  margin-top: 0px;
  background-color: white;
  box-sizing: border-box;
  padding: 30px 30px 0 30px;
  .password-login-title {
    width: 100%;
    height: 24px;
    font-size: 24px;
    font-weight: 500;
    color: #171923;
    line-height: 24px;
    box-sizing: border-box;
    margin-bottom: 30px;
  }

  .item {
    width: 100%;
    height: auto;
    float: left;
    box-sizing: border-box;
    padding: 6px 0;
    display: flex;
    border-bottom: 1px solid #f4faff;
    margin-bottom: 50px;

    .name {
      min-width: 48px;
      height: auto;
      font-size: 16px;
      font-weight: 400;
      color: #333333;
      line-height: 36px;
      margin-right: 25px;
    }

    .input {
      flex: 1;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      box-sizing: border-box;
      align-items: center;

      .input-text {
        max-width: 200px;
        height: 36px;
        float: left;
        box-sizing: border-box;
        outline: none;
        color: #333333;
        font-size: 16px;
        border: none;
      }
    }
    .captcha {
      width: 90px;
      height: 36px;
      padding-top: 0px;
      margin-left: 19px;
      img {
        width: 90px;
        display: inline-block;
        overflow: hidden;
        position: relative;
      }
    }
  }
}
</style>

<style>
#page {
  background-color: #f8f8f8;
}
</style>
